<?php

class Pelicula {
	/**
	 * Zend_Db_Adapter
	 * @var $db
	 * 	 
	 */	
	protected $db = null;	
	protected $logger = null;
	
	function __construct() {
		$this->db = Zend_Registry::get("database");
		$this->logger = Zend_Registry::get("logger");
	}
	
	public function verMas($idPelicula){
		try {			
			$sql = 'SELECT *
					FROM Pelicula p 
					JOIN genero g USING (idGenero) 
					JOIN clasificacion USING (idClasificacion)
					JOIN multimedia m USING (idPelicula) 
					WHERE p.idPelicula = ?';
			$result  = $this->db->fetchAll($sql, array($idPelicula));
			if(count($result)>0){
				return $result[0];				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
	}
	
	public function getCatalogo(){
		try {			
			$sql = 'SELECT p.idPelicula IDP, Gnombre, Cnombre, Pnombre, Mlink
					FROM Pelicula p 
					JOIN genero g USING (idGenero) 
					JOIN clasificacion USING (idClasificacion)
					JOIN multimedia m USING (idPelicula) 
					WHERE Mformato = "imagen"';
			$result  = $this->db->fetchAll($sql);
			if(count($result)>0){
				return $result;				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
		
	}
	
	public function getCartelera(){
		
		try {			
			$sql = 'SELECT P.Pnombre, M.Mformato, M.Mlink  
					FROM Cartelera AS CA 
					JOIN PxCA using(idCartelera) 
					JOIN Pelicula AS P USING(idPelicula) 
					JOIN Multimedia AS M USING(idPelicula) 
					WHERE CURDATE() BETWEEN  CAdateIni AND CAdateFin';
			$result  = $this->db->fetchAll($sql);
			if(count($result)>0){
				return $result;				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
		
	}
	
	public function getGeneros(){
		try {			
			$sql = 'SELECT idGenero, Gnombre FROM genero';
			$result  = $this->db->fetchAll($sql);
			if(count($result)>0){
				return $result;				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
		
	}
	
	public function getClasificaciones(){
		try {			
			$sql = 'SELECT idClasificacion, Cnombre, Cvista  FROM clasificacion';
			$result  = $this->db->fetchAll($sql);
			if(count($result)>0){
				return $result;				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
		
	}
	
	public function getPaises(){
		try {			
			$sql = 'SELECT idPais, Pnombre FROM pais';
			$result  = $this->db->fetchAll($sql);
			if(count($result)>0){
				return $result;				
			}
		} catch (Exception $e) {
			$this->logger->info($e);
		}
		
		return null;
		
	}
	
	public function savePelicula($genero, $clasificacion, $pais, $nombre, $psinopsis, $duracion, $caracteristicas, $formatoP, $fecha, $formato, $link, $insertar, $descripcion){
		try {			
			// guardar en la bd
			$insert ='INSERT INTO pelicula (idGenero, idClasificacion, idPais, Pnombre, Psinopsis, Pduracion, Pcaracteristicas, Pformato, Pfecha) VALUES ('.$genero.','. $clasificacion.','. $pais.',"'. $nombre.'","'. $psinopsis.'","'. $duracion.'","'. $caracteristicas.'","'. $formatoP.'","'. $fecha.'")';
			$stmt = $this->db->prepare($insert);	
			$stmt->execute();
			$ultimoID = $this->db->lastInsertid();
			$insertMultimedia = 'INSERT INTO multimedia (idPelicula, Mformato, Mlink, Minsertar, Mdescripcion) VALUES ('.$ultimoID.', "'.$formato.'", "'.$link.'", "'.$insertar.'", "'.$descripcion.'")';
			$stmtMul = $this->db->prepare($insertMultimedia);	
			$stmtMul->execute();
			$ultimoIDMul = $this->db->lastInsertid();
			if ($ultimoIDMul != null) {
				return true;
			}
		} catch (Exception $e) {
			$this->logger->info($e);
			return null;
		}
		
		return false;
	}
}

?>